<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

class admin_controller_config extends omnitrix_controller
{
	public function index_action()
	{
		if (!otx_session_permission('admin_config')) {
			otx_admin_critical_type('invalid_permission');
		}

		$all_configs = $this->_fetch_all_configs();

		$list_table = otx_html_create_table($this->_('heading_index'), 'table-list');
		$list_table->set_head_cell($this->_('column_config_group'));

		foreach ($all_configs as $group_name => $group_info) {
			$pos = strpos($group_info['title'], '<p>');
			if (false !== $pos) {
				$group_detail = otx_html_link(otx_html_strong(substr($group_info['title'], 0, $pos)), array('href' => otx_router_url('edit', null, null, array('group_name' => $group_name))))
						. substr($group_info['title'], $pos);
			} else {
				$group_detail = otx_html_link(otx_html_strong($group_info['title']), array('href' => otx_router_url('edit', null, null, array('group_name' => $group_name))));
			}

			$list_table->add_body_row();
			$list_table->set_body_cell($group_detail);
		}

		otx_html_add_navtab($this->_('navtab_index_title'), $this->_('navtab_index_desc'), otx_router_url('index'), true);
		otx_html_add_breadcrumb($this->_('breadcrumb_index'));
		otx_html_response_title($this->_('breadcrumb_index'));
	}

	public function edit_action()
	{
		$group_name = otx_param_string('group_name');

		$all_configs = $this->_fetch_all_configs();
		$group_info = $all_configs[$group_name];

		$pos = strpos($group_info['title'], '<p>');
		if (false !== $pos) {
			$group_title = substr($group_info['title'], 0, $pos);
			$group_desc = substr($group_info['title'], $pos + 3, strlen($group_info['title']) - $pos - 7);
		} else {
			$group_title = $group_info['title'];
			$group_desc = $group_info['title'];
		}

		if (otx_request_is_post()) {
			foreach ($group_info['items'] as $config_name => $config_info) {
				$config_value = otx_param_string($config_name, '0');
				otx_db_replace('#system_config#', array('name' => $config_name, 'value' => $config_value, 'serialized' => 0));
			}

			otx_admin_flash_message($this->_('text_success_save', $group_title));
			otx_admin_log_action($this->_('text_success_save', $group_title));
			otx_router_redirect('index');
		}

		$form_table = otx_html_create_wrapper()->create_table($group_title, 'table-form');
		foreach ($group_info['items'] as $config_name => $config_info) {
			if (empty($config_info['style'])) {
				$config_info['style'] = 'text';
			}

			if (!array_key_exists('default', $config_info)) {
				$config_info['default'] = '';
			}

			if (!array_key_exists('attribs', $config_info)) {
				$config_info['attribs'] = array();
			}

			if (array_key_exists($config_name, $GLOBALS['_XCONFIG'])) {
				$config_value = $GLOBALS['_XCONFIG'][$config_name];
			} else {
				$config_value = $config_info['default'];
			}

			$function_name = 'otx_html_form_' . $config_info['style'];
			if (function_exists($function_name)) {
				$config_input = call_user_func($function_name, $config_name, $config_value, $config_info['attribs']);
			} else {
				$config_input = otx_html_form_text($config_name, $config_value, $config_info['attribs']);
			}

			if (array_key_exists('generator', $config_info)) {
				$generator_url = otx_router_url('generator', null, null, array('group_name' => $group_name, 'config_name' => $config_name));
				$config_input .= '&nbsp;' . otx_html_link($this->_('text_generator'), array('class' => 'pointer', 'onclick' => "AdminCP.generator('$config_name', '$generator_url')"));
			}

			$form_table->add_form_entry($config_info['title'], $config_input);
		}

		otx_html_add_navtab($this->_('navtab_index_title'), $this->_('navtab_index_desc'), otx_router_url('index'));
		otx_html_add_navtab($group_title, $group_desc, otx_request_uri(), true);
		otx_html_add_breadcrumb($this->_('breadcrumb_index'), otx_router_url('index'));
		otx_html_add_breadcrumb($group_title);
		otx_html_response_title($group_title);
	}

	protected function _fetch_all_configs()
	{
		$event = otx_event_dispatch('admin_configs', array('admin_configs' => array()));
		return $event->admin_configs;
	}

	public function generator_action()
	{
		$group_name = otx_param_string('group_name');
		$config_name = otx_param_string('config_name');

		$all_configs = $this->_fetch_all_configs();
		$config_info = $all_configs[$group_name]['items'][$config_name];

		$length = $config_info['generator']['length'];
		otx_json_result(otx_string_random($length));
	}
}